<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/10/17 0017
  Time: 上午 10:42
  To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>车辆保险--添加页</title>
    <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
    <%--<link rel="stylesheet" href="/static/css/carcom.css">--%>
</head>
<body>
<div class="wrap_padding35">
    <form class="layui-form" action="javascript:">
        <div class="layui-form-item">
            <label class="layui-form-label">车牌号码：</label>
            <div class="layui-input-block">
                <input type="text" name="carcode" lay-verify="required|carCode" placeholder="请输入车牌号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">保险公司：</label>
            <div class="layui-input-block">
                <input type="text" name="company" lay-verify="required" placeholder="请输入保险公司名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">出险电话：</label>
            <div class="layui-input-block">
                <input type="text" name="companyphone" lay-verify="required|phone" placeholder="请输入出险电话" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">保单编号：</label>
            <div class="layui-input-block">
                <input type="text" name="code" lay-verify="required" placeholder="请输入保单编号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">保单内容：</label>
            <div class="layui-input-block">
                <input type="text" name="content" lay-verify="required" placeholder="请输入保单内容" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">保单费用：</label>
            <div class="layui-input-block">
                <input type="text" name="money" lay-verify="required|number" placeholder="请输入保单费用" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">生效日期：</label>
                <div class="layui-input-inline">
                    <input type="text" name="validdate" class="layui-input" id="begin" placeholder="请选择生效日期">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">失效日期：</label>
                <div class="layui-input-inline">
                    <input type="text" name="invaliddate" class="layui-input" id="end" placeholder="请选择失效日期">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">经办人员：</label>
            <div class="layui-input-block">
                <input type="text" name="contactman" lay-verify="required" placeholder="请输入经办人员" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">经办手机：</label>
            <div class="layui-input-block">
                <input type="text" name="contactmobile" lay-verify="required|phone" placeholder="请输入经办手机" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注信息：</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入备注信息" class="layui-textarea" name="remark"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="insurance">立即提交</button>
                <button type="button" class="layui-btn layui-btn-primary"><a href="/insurance" target="_parent">取消</a></button>
            </div>
        </div>
    </form>
</div>

<script src="/static/plugins/jquery.js"></script>
<script src="/static/plugins/layui/layui.js"></script>

<%--2、引入bootstrap组件--%>
<script src="/static/plugins/bootstrap/js/bootstrap.js"></script>
<%--3、bootstrap table组件以及中文包的引用--%>
<script src="/static/plugins/bootstrap-table/bootstrap-table.js"></script>
<script src="/static/plugins/bootstrap-table/bootstrap-table-zh-CN.js"></script>
<script>
    layui.use(["layer","form","element","laydate"],function () {
        var form = layui.form;
        var laydate = layui.laydate;
        var layer = layui.layer;
        laydate.render({
            elem: '#begin'
            ,type:"datetime"
        });

        laydate.render({
            elem:"#end"
            ,type:"datetime"
        });

        form.verify({
            carCode:function (value) {                      //carCode 不是name！
                var unique = false;
                $.ajax({
                    url:'/cars/carCodeCheck',
                    data:{carcode:value},
                    dataType:'json',
                    async:false,
                    success:function(msg){
                        if (msg.code == 1){             //如果为1，表示该车牌存在
                            unique = msg.data
                        }
                    },error:function () {
                        layer.msg("服务器错误")
                    }
                });
                if (!unique){
                    return "该车牌不存在";
                }
            }
        });

        form.on('submit(insurance)', function(data){
            $.ajax({
                url:"/insurance/save",
                type:"post",
                data:data.field,
                dataType:"json",
                success:function (msg) {
                    if(msg.code == 1){
                        layer.msg(msg.message,{time:1000,icon:1},function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.$("#tb_car_insurance").bootstrapTable("refresh"); // 刷新表格数据
                            parent.layer.close(index); //再执行关闭
                        })
                    } else {
                        layer.msg(msg.message,{time:3000,icon:2});
                    }
                },
                error:function () {
                    console.log("异步方法错误！")
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    })
</script>
</body>
</html>
